Rem   Auto corner finding X+Y-Z-(button2) With tool diameter Input Metric Version

CurrentFeed = GetOemDRO(818) 'Get the current feedrate to return to later
CurrentAbsInc = GetOemLED(48) 'Get the current G90/G91 state
CurrentGmode = GetOemDRO(819) 'Get the current G0/G1 state
CurrentToolDiameter = GetOemDRO(43) 'gets the current tool diameter

If GetOemLed (825)<>0 Then 'Check to see if the probe is already grounded or faulty
	Call ProbeGrounded()
	Exit Sub
Else
	DoOEMButton (1010) 'zero the Z axis so the probe move will start from here
	Code "G4 P2" ' two second delay
	Code "G90 G31 Z-25 F100" 'Z goes down a max of 25mm at 100mm/sec
	While IsMoving() 'wait while it happens
	Wend
	ZProbePos = GetVar(2002) 'get the axact point the probe was hit
	Code "G0 Z" &ZProbePos 'go back to that point, always a very small amount of overrun
	While IsMoving ()
	Wend
	Call SetDro (2,0.000) 
	Code "G4 P0.25" 'Pause for Dro to update.
	Code "G90 G0 Z15" 'retract Z to 15mm
	Code "G91 G0 Y3 X-25"
	Code "G90 G0 Z-5"
	Code "F" &CurrentFeed 'Returns to prior feed rate
End If

Rem	VBScript For probing In the x+ direction

If GetOemLed (825)<>0 Then 		'May need to use =0 if it errors
	Call ProbeGrounded()           'Check to see if the probe is already grounded or faulty
	Exit Sub
Else
	Code "G4 P1"			'Pause 1 second to give time to position probe plate

	CurrentFeed = GetOemDRO(818) 	'Get the current feedrate to return to later
	Code "F100"

Rem	Probe Right

	XNew = GetDro(0) + 75		'probe to current position + 75mm
	Code "G31 X" &XNew
	While IsMoving()		'wait for prob move to complete
	Wend
	XNew = GetVar(2000)		'read the touch point

Rem	move back To the hit point incase there was an overshoot

	Code "G90 G0 Z15"
	Code "G90 G0 X" &XNew
	Code "G91 X" &CurrentToolDiameter/2  'moves the probe half the tool diameter to center on edge
        
	
	While IsMoving ()
	Wend
        Call SetDro (0,0.000)
        Code "G4 P0.25" 
        Code "G91 G0 X3 Y25"
        Code "G90 G0 Z-5"
	Code "F" &CurrentFeed 		'restore original feed rate
End If 

Rem	VBScript For probing In the Y- direction

If GetOemLed (825) <> 0 Then 		'Check to see if the probe is already grounded or faulty
	Call ProbeGrounded()
	Exit Sub
Else
	Code "G4 P1"			'Pause 1 second to give time to position probe plate

	CurrentFeed = GetOemDRO(818) 	'Get the current feedrate to return to later
	Code "F100"			'Slow feedrate to 4 ipm

Rem	Probe up

	YNew = GetDro(1) - 75		'move to current y position + 75mm
	Code "G31 Y" &YNew
	While IsMoving()		'wait for the move to finish
	Wend
	YNew = GetVar(2001)		'read the touch point

Rem	move back To the hit point incase there was an overshoot
	
        Code "G90 G0 Z15"
	Code "G90 G0 Y" &YNew
        Code "G91 Y-" &CurrentToolDiameter/2  'moves the probe half the tool diameter to center on edge

	While IsMoving ()
	Wend
        Call SetDro (1,0.000)
        Code "G4 P0.25"
        Code "G90 G0 X0.0"
	Code "F" &CurrentFeed 		'restore original feed rate
        Code "(X, Y, and Z axis' are now zeroed)"
        Call ReturnG90G91State()
        Exit Sub
End If

Sub ProbeGrounded()
	Code "(Probe plate is grounded, check connection and try again)"
	Call ReturnG90G91State()
End Sub

Sub ReturnG90G91State()
	If CurrentAbsInc = 0 Then 'if G91 was in effect before then return to it
		Code "G91"
	End If
	If CurrentGMode = 0 Then 'if G0 was in effect before then return to it
		Code "G0"
	End If
End Sub    

